// import { _decorator, Component, director, rect, Rect, v2, Vec2, Vec3 } from "cc";

// const { ccclass, property } = _decorator;
// const STEP = 0.1;  // 碰撞检测步长
// const DEVIATION = 0.02;  // 碰撞检测误差

// @ccclass
// export default class PlayerController extends Component {
//     // 碰撞体尺寸
//     private normalHitbox: Rect = rect(0, -0.25, 0.8, 1.1);
//     private duckHitbox: Rect = rect(0, -0.5, 0.8, 0.6);
    
//     private collider: Rect = this.normalHitbox;
//     private position: Vec2 = Vec2.ZERO;
//     private speed: Vec2 = Vec2.ZERO;
//     private facing: number = 1; // 1=右, -1=左

//     // 地面检测层
//     @property(PhysicsCollider)
//     groundCollider: PhysicsCollider = null;

//     // 调整位置（外部调用）
//     public adjustPosition(adjust: Vec2): void {
//         this.updateCollideX(adjust.x);
//         this.updateCollideY(adjust.y);
//     }

//     // 碰撞检测
//     private collideCheck(position: Vec2, dir: Vec2, dist: number = 0): boolean {
//         const origin = position.add(v2(this.collider.x, this.collider.y));
//         const checkPos = origin.add(dir.mul(dist + DEVIATION));
//         return this.overlapBox(checkPos, this.collider);
//     }

//     // 点检测
//     private overlapPoint(position: Vec2): boolean {
//         const results: PhysicsCollider[] = [];
//         director.getPhysicsManager().testPoint(position, results);
//         return results.some(collider => collider === this.groundCollider);
//     }

//     // 检测是否在地面上
//     private checkGround(offset: Vec2 = Vec2.ZERO): boolean {
//         const origin = this.position.add(v2(this.collider.x, this.collider.y)).add(offset);
//         const hit = this.boxCast(origin, this.collider, v2(0, -1), DEVIATION);
//         return hit && hit.normal.equals(v2(0, 1));
//     }

//     // 横向碰撞调整
//     private updateCollideX(distX: number): void {
//         let distance = distX;
//         let correctTimes = 1;
        
//         while (true) {
//             const moved = this.moveXStepWithCollide(distance);
//             this.position.x += moved;
            
//             if (moved === distance || correctTimes === 0) {
//                 break;
//             }
            
//             const tempDist = distance - moved;
//             correctTimes--;
            
//             if (!this.correctX(tempDist)) {
//                 this.speed.x = 0;
//                 break;
//             }
            
//             distance = tempDist;
//         }
//     }

//     // 纵向碰撞调整
//     private updateCollideY(distY: number): void {
//         let distance = distY;
//         let correctTimes = 1;
//         let collided = true;
//         const speedY = Math.abs(this.speed.y);
        
//         while (true) {
//             const moved = this.moveYStepWithCollide(distance);
//             this.position.y += moved;
            
//             if (moved === distance || correctTimes === 0) {
//                 collided = false;
//                 break;
//             }
            
//             const tempDist = distance - moved;
//             correctTimes--;
            
//             if (!this.correctY(tempDist)) {
//                 this.speed.y = 0;
//                 break;
//             }
            
//             distance = tempDist;
//         }
        
//         // 落地检测
//         if (collided && distY < 0) {
//             // 播放落地特效
//         }
//     }

//     // X轴单步移动
//     private moveXStepWithCollide(distX: number): number {
//         const direction = Math.sign(distX) > 0 ? v2(1, 0) : v2(-1, 0);
//         const origin = this.position.add(v2(this.collider.x, this.collider.y));
        
//         const hit = this.boxCast(origin, this.collider, direction, Math.abs(distX) + DEVIATION);
        
//         if (hit && hit.normal.equals(direction.mul(-1))) {
//             return direction.mul(Math.max(hit.distance - DEVIATION, 0)).x;
//         } else {
//             return distX;
//         }
//     }

//     // Y轴单步移动
//     private moveYStepWithCollide(distY: number): number {
//         const direction = Math.sign(distY) > 0 ? v2(0, 1) : v2(0, -1);
//         const origin = this.position.add(v2(this.collider.x, this.collider.y));
        
//         const hit = this.boxCast(origin, this.collider, direction, Math.abs(distY) + DEVIATION);
        
//         if (hit && hit.normal.equals(direction.mul(-1))) {
//             return direction.mul(Math.max(hit.distance - DEVIATION, 0)).y;
//         } else {
//             return distY;
//         }
//     }

//     // X轴校正
//     private correctX(distX: number): boolean {
//         // 简化的校正逻辑，实际项目中需根据游戏需求实现
//         return false;
//     }

//     // Y轴校正
//     private correctY(distY: number): boolean {
//         // 简化的校正逻辑，实际项目中需根据游戏需求实现
//         return false;
//     }

//     // 辅助方法：模拟BoxCast
//     private boxCast(origin: Vec2, size: Rect, direction: Vec2, distance: number): any {
//         // 实际项目中需要实现物理检测逻辑
//         // 这里使用简化的伪代码
//         const results: any[] = [];
//         director.getPhysicsManager().testBox(origin, size, direction, distance, results);
//         return results.length > 0 ? results[0] : null;
//     }

//     // 辅助方法：模拟OverlapBox
//     private overlapBox(center: Vec2, size: Rect): boolean {
//         // 实际项目中需要实现物理检测逻辑
//         const results: PhysicsCollider[] = [];
//         director.getPhysicsManager().testBox(center, size, results);
//         return results.some(collider => collider === this.groundCollider);
//     }

//     // 更新位置（每帧调用）
//     update(dt: number): void {
//         // 状态机更新...
//         this.updateCollideX(this.speed.x * dt);
//         this.updateCollideY(this.speed.y * dt);
//         this.node.position = new Vec3(this.position.x, this.position.y, 0);
//     }
// }